package com.agile.leetcode.spi.client;

import com.agile.leetcode.spi.Human;
import com.sun.media.jai.util.Service;


import java.io.IOException;
import java.sql.*;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/3/24 10:38
 */
public class Client {
    public static void main(String[] args) throws SQLException, IOException {
        // 加载
        ServiceLoader<Human> serviceLoader = ServiceLoader.load(Human.class);
        // 拿到迭代器
        Iterator<Human> iterator = serviceLoader.iterator();
        while (iterator.hasNext()) {
            Human human = iterator.next();
            System.out.println(human.getGender());
        }
        // 拿到迭代器
        Iterator<Human> service = Service.providers(Human.class);
        while (service.hasNext()) {
            System.out.println(service.next().getGender());
        }


        // 数据库驱动 Driver的spi实现
        Iterator<Driver> providers = Service.providers(Driver.class);
        while (providers.hasNext()) {
            System.out.println(providers.next().getClass());
        }
        // 连接数据库
        Connection connect = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bos32?characterEncoding=UTF-8&useSSL=false", "root", "1234");
        Statement statement = connect.createStatement();
        ResultSet resultSet = statement.executeQuery("select * from course where cid =1");
        while (resultSet.next()) {
            String string = resultSet.getString(2);
            System.out.println(string);
        }
    }
}
